/*
 * @Author: w
 * @Date: 2022-02-18 15:51:43
 * @Description: 剑指 Offer 30. 包含min函数的栈
 */

// 缓存栈  min()时间复杂度O(1) 则需要push、pop处理minV值

/**
 * initialize your data structure here.
 */
var MinStack = function () {
  this.stack = [];
  this.min_stack = []; // 辅助栈
};

/**
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function (x) {
  this.stack.push(x);
  const min = Math.min(x, this.min_stack[this.min_stack.length - 1]);
  this.min_stack.push(min);
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {
  this.min_stack.pop();
  this.stack.pop();
};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {
  return this.stack[this.stack.length - 1];
};

/**
 * @return {number}
 */
MinStack.prototype.min = function () {
  return this.min_stack[this.stack.length - 1];
};

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(x)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.min()
 */
